package com.market.cash.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.market.cash.utils.log.CashRootLog;
import com.market.cash.weight.toast.CustomToast;

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/9/03 10:46
 * @Annotation：管理toast的类
 */
public class CashRootToastUtil {

    protected static final String TAG = "ToastManager";
    public static CustomToast toast;

    /**
     * 信息提示
     *
     * @param context
     * @param content
     */
    public static void makeToast(Context context, String content) {
        if (context == null)
            return;
        if (toast != null)
            toast.cancel();
        toast = new CustomToast(context, content, Toast.LENGTH_LONG);
        toast.show();
    }

    public static void showShortText(Context context, int resId) {
        try {
            if (context == null)
                return;
            if (toast != null)
                toast.cancel();
            toast = new CustomToast(context, context.getString(resId), Toast.LENGTH_SHORT);
            toast.show();
        } catch (Exception e) {
            CashRootLog.e(e.getMessage());
        }
    }

    public static void showShortText(Context context, CharSequence text) {
        if (context == null)
            return;
        if (toast != null)
            toast.cancel();
        toast = new CustomToast(context, text.toString(), Toast.LENGTH_SHORT);
        toast.show();
    }

    public static void showLongText(Context context, int resId) {
        try {
            if (context == null)
                return;
            if (toast != null)
                toast.cancel();
            toast = new CustomToast(context, context.getString(resId),
                    Toast.LENGTH_LONG);
            toast.show();

        } catch (Exception e) {
            CashRootLog.e(e.getMessage());
        }
    }

    public static void showLongText(Context context, CharSequence text) {
        if (context == null)
            return;
        if (toast != null)
            toast.cancel();
        toast = new CustomToast(context, text.toString(), Toast.LENGTH_LONG);
        toast.show();
    }

    public static void showTextDuration(final Context context, final int resId, final int time) {
        try {
            if (context == null)
                return;
            if (toast != null)
                toast.cancel();
            toast = new CustomToast(context, context.getString(resId), time);

            ((Activity) context).runOnUiThread(new Runnable() {
                public void run() {
                    toast.show();
                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        public void run() {
                            toast.cancel();
                        }
                    }, time);
                }
            });
        } catch (Exception e) {
            CashRootLog.e(e.getMessage());
        }
    }

    public static void showTextDuration(Context context, CharSequence text, final int time) {
        if (context == null)
            return;
        if (toast != null)
            toast.cancel();
        toast = new CustomToast(context, text.toString(), time);

        ((Activity) context).runOnUiThread(new Runnable() {
            public void run() {
                toast.show();
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    public void run() {
                        toast.cancel();
                    }
                }, time);
            }
        });
    }
}
